/*
 *评论
 * @returns {[[Type]]} [[Description]]
 */
define([
        '../../common/service',
        './commentTpl'
    ],
    function(
        http,
        commentTpl
    ) {

        var Comment = function(mongodId) {
            var self = this;
            self.mongodId = mongodId;
            self.init();
        };


        Comment.prototype = $.extend(Comment.prototype, {
            init: function() {
                var self = this;
                if ($("#comment").length > 0) {
                    $("#comment").data("mid", self.mongodId);
                    $("#comment").dialog({
                        zIndex: 4000000
                    });
                    $("#comment").dialog('moveToTop');
                    $("#comment").dialog("open");
                    return;
                }

                var tmpl = ' <div>\
                <div class="well">\
                <section class="">\
                <div class="chat-body custom-scroll" style="height:200px;overflow-x: hidden;">\
                <ul id="commentsObjHtml"></ul>\
                </div>\
                <div style="display:inline-block;margin-top: 10px;">\
                <div style="float:left"><label style="padding-top:7px;"><strong>' + I18N.getText('common', 'commentsTemplate') + '</strong></label></div>\
                <div style="float:left;width:568px;margin-left:10px"><select style="width:100%" id="comment-template"/></div>\
                </div>\
                <div style="margin-top: 5px;">\
                <textarea class="form-control" name="commentBody" maxlength="100" style="height:100px;width:632px;line-height: 28px;" placeholder="' + I18N.getText('common', 'enterComments') + '..."></textarea>\
                </div>\
                </section>\
                </div>\
                </div>';



                $('#comment-image-modal .modal-body').html(tmpl);


                //初始化评语模板
                $("#comment-template").select2();
                $("#comment-template").select2('data', commentTpl);

                $("#comment-template").on("change", function(e) {
                    if ($(this).val() == '' || $(this).val() == '-1') {
                        return;
                    }
                    var val = $(this).val();
                    var text = $("#comment-template").find('[value=' + val + ']').text()
                    var _commentArea = $('textarea[name="commentBody"]');
                    var _comment = $.trim(_commentArea.val());
                    _comment = _comment == '' ? _comment + text : _comment + ' ' + text;
                    _commentArea.val(_comment);
                    _commentArea.focus();
                    $("#comment-template").select2("val", "-1");
                }).select2("val", "-1");

                var par = {
                    mongodId: self.mongodId
                };
                http.do('/adresource/getCommentsById', {
                    param: JSON.stringify(par)
                }, 'POST').then(function(result) {
                    if (result.flag == 'fail') {
                        kit.alert2(result.msg);
                        return;
                    }
                    //清空评语
                    $('textarea[name="commentBody"]').val('').focus();
                    //清空评语列表
                    $("#commentsObjHtml").empty();
                    //清空模板列表
                    $("#comment-template").select2("val", "-1");
                    if (result.data.comments) {
                        var comment = JSON.parse(result.data.comments);
                        $.each(comment, function(key, val) {
                            var everyCommentObj = JSON.parse(val);
                            $("#commentsObjHtml").append('<li class="message"><div class="message-text" style="margin-left:0px;"><div class="row"><div class="col-md-12"><time class="pull-right">' + (everyCommentObj.commentDate ? moment(everyCommentObj.commentDate).format('YYYY-MM-DD HH:mm:ss') : '') + '</time><a href="javascript:void(0);" class="username">' + everyCommentObj.username + '</a></div></div>' + everyCommentObj.commentBody + '</div></li>');
                        });
                    };
                    $('#comment-image-modal').modal('show');
                    if (window.sessionStorage.roleId == '6') {
                        $('#comment-image-modal .chat-body').next().hide();
                        $('#comment-image-modal .chat-body').next().next().hide();
                        $('#comment-image-modal .btn-primary').hide();
                    }

                }, function(msg, data, type, errorCode) {
                    kit.alert2(I18N.getText('common', 'failedToList'));
                });

                $('#comment-image-modal .btn-primary').off('click').on('click', function(e) {
                    var par = {
                        mongodbId: self.mongodId
                    };

                    var cmt = $('textarea[name="commentBody"]');
                    var _cmt = $.trim(cmt.val());
                    if (_cmt.length < 1) {
                        cmt.val('');
                        cmt.testRemind(I18N.getText('imageReview', 'pleaseEnterComment'));
                        cmt.focus();
                        return false;
                    }
                    if (_cmt.length > 100) {
                        cmt.testRemind(I18N.getText('imageReview', 'maxWordsNumber', 100));
                        cmt.focus();
                        return false;
                    }
                    par.commentBody = _cmt;

                    http.do('/adresource/saveComment', {
                        param: JSON.stringify(par)
                    }, 'POST').then(function(result) {
                        if (result.flag == 'fail') {
                            kit.alert2(result.msg);
                            return;
                        }
                        kit.alert2(I18N.getText('imageReview', 'evaluationOfSuccess'));
                        $("#commentsObjHtml").append('<li class="message"><div class="message-text" style="margin-left:0px;"><a href="javascript:void(0);" class="username">' + window.sessionStorage.username + '</a>' + cmt.val() + '</div></li>');
                        $('#comment-image-modal').modal('hide');
                    }, function(msg, data, type, errorCode) {
                        kit.alert2(I18N.getText('common', 'failedToList'));
                        $('#comment-image-modal').modal('hide');
                    });
                });

            }
        });

        return Comment;
    });
